package com.jkoss.wine.shop.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.merchant.vo.MerchantProductVo;
import com.jkoss.wine.merchantSetting.entity.MerchantAsset;
import com.jkoss.wine.shop.entity.MerchantCommonType;
import com.jkoss.wine.shop.service.IMerchantCommonTypeService;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 前端控制器
 * 
 * @Author Supreme
 * @Version 1.0, 2018-09-29
 * @See
 * @Since com.jkoss.wine.shop.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/shop/merchantCommonType")
public class MerchantCommonTypeController extends BaseController {

	@Autowired
	private IMerchantCommonTypeService iMerchantCommonTypeService;

	/**
	 * 列表
	 * 
	 * @param typeName
	 * @param typeClass
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/shop/merchantCommonType/list")
	public String list(String typeName, Integer typeClass, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {

		dwzPageBean.getCountResultMap().put("typeClass", typeClass);
		dwzPageBean.getCountResultMap().put("typeName", typeName);

		Wrapper wrapper = Condition.create().ne("del", 1);
		if (!CommonUtil.isBlank(typeClass)) {
			wrapper.eq("type_class", typeClass);
		}
		if (!CommonUtil.isBlank(typeName)) {
			wrapper.like("type_name", typeName);
		}
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}
		Page resultPage = iMerchantCommonTypeService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "shop/merchantCommonType/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/shop/merchantCommonType/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("del", 1);
		iMerchantCommonTypeService.selectList(wrapper);
		return "shop/merchantCommonType/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/shop/merchantCommonType/toInsert")
	@ResponseBody
	public Object insert(@Valid MerchantCommonType merchant_common_type, HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 查询重复
		Wrapper wrapper = Condition.create().eq("type_class", merchant_common_type.getTypeClass()).eq("type_name", merchant_common_type.getTypeName()).ne("del", 1);
		if (!CommonUtil.isBlank(iMerchantCommonTypeService.selectList(wrapper))) {
			return ajaxError("名字重复");
		}
		// 获取当前时间
		merchant_common_type.setAddTime(CommonUtil.getDate());
		// 获取当前操作人，当操作人为null时，表示自动录入
		MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		merchant_common_type.setOperator(employees.getEmployeeId());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		merchant_common_type.setDel(0);
		if (iMerchantCommonTypeService.insertCommonType(merchant_common_type)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/shop/merchantCommonType/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iMerchantCommonTypeService.selectById(id));
		return "shop/merchantCommonType/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/shop/merchantCommonType/toUpdate")
	@ResponseBody
	public Object update(@Valid MerchantCommonType merchant_common_type, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// if (iMerchantCommonTypeService.updateById(merchant_common_type)) {
		// return ajaxSuccess();
		// } else {
		// return ajaxError();
		// }
		if (iMerchantCommonTypeService.updateCommonType(merchant_common_type)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/shop/merchantCommonType/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) throws Exception {

		/*
		 * if (iMerchantCommonTypeService.updateBatchById(list)) { return ajaxSuccess();
		 * } else { return ajaxError(); }
		 */
		List list = new ArrayList();
		for (String string : id) {
			MerchantCommonType merchantCommonType = new MerchantCommonType();
			merchantCommonType.setCommonTypeId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			merchantCommonType.setDel(1);
			list.add(merchantCommonType);
		}
		if (iMerchantCommonTypeService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 导出通用类型设置Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/MerchantCommonTypeexport")
	@RequiresPermissions("/shop/merchantCommonType/MerchantCommonTypeexport")
	public String MerchantCommonTypeexport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iMerchantCommonTypeService.selectPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "通用类型设置", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, MerchantCommonType.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "通用类型设置");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

}
